#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, const char **argv) {
	if (argc != 6) {
		fprintf(stderr, "usage: x86_64-construct-vaddr <pml4e> <pdpte> <pde> <pte> <bi>\n");
		return 1;
	}
	uint64_t pml4e = strtoull(argv[1], NULL, 0),
		pdpte = strtoull(argv[2], NULL, 0),
		pde = strtoull(argv[3], NULL, 0),
		pte = strtoull(argv[4], NULL, 0),
		bi = strtoull(argv[5], NULL, 0);
	uint64_t addr = (pml4e << 39) + (pdpte << 30) + (pde << 21)
		+ (pte << 12) + bi;
	// tricky sign extension
	addr = addr | (addr & 0x0000800000000000L) * 0x1ffff;
	printf("0x%016lx\n", addr);
}
